/**
 * Copyright @ 2000 ysten Co. Ltd. 
 * All right reserved.     
 * @author: wangmin
 * date: 2016-07-11     
 */
package com.task.controller;

import java.util.Map;

import javax.servlet.ServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.task.bean.Constants;
import com.task.entity.Message;
import com.task.service.IMessageService;

import net.sf.json.JSONObject;

/**
 * 消息控制器
 */
@Controller
@RequestMapping("/userMsg")
public class MessageController
{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageController.class);
    
    @Autowired
    IMessageService messageService;
    
    @ResponseBody
    @RequestMapping(value = "/findUserMsg.json")
    public JSONObject findUserMsg(@RequestParam(value = "page", defaultValue = Constants.DEFAULT_PAGE) int pageNum,
        @RequestParam(value = "rows", defaultValue = Constants.DEFAULT_ROWS) int rows, ServletRequest request)
    {
        JSONObject jsonObject = new JSONObject();
        try
        {
            Map<String, Object> searchParams = WebUtils.getParametersStartingWith(request, Constants.SEARCH_PREFIX);
            Page<Message> page = messageService.findMsgPage(searchParams, pageNum, rows);
            if (page != null)
            {
                jsonObject.put(Constants.ROWS, page.getContent());
                jsonObject.put(Constants.TOTAL, page.getTotalElements());
            }
        }
        catch (Exception e)
        {
            LOGGER.error(" findUserMsg error:{}", e);
        }
        return jsonObject;
    }
    
}
